package com.asf.sofom.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.asf.sofom.domain.OPersona;

@Repository(value="productDao")
public class JPAPersona implements PersonaDao{

	private EntityManager em = null;
	
	@PersistenceContext
	public void setEntityManager(EntityManager em){
		this.em = em;
	}
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<OPersona> getPersonas() {
		return em.createQuery("Select * from personas").getResultList();
		
	}

	@Transactional(readOnly = false)
	public void guardaPersona(OPersona persona) {
		em.merge(persona);
	}

	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<OPersona> buscaPersona(OPersona persona) {
		TypedQuery<OPersona> tq = (TypedQuery<OPersona>) em.createNativeQuery("Select * from personas where id_persona='" + persona.getId() + "'", OPersona.class);
		List<OPersona> lp = tq.getResultList();			
		return lp;
	}
	

}
